package com.bootcamp.javase2.threads;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * @description:
 * @author: 305905917@qq.com
 **/
public class CodeZY {
    public static void main(String[] args) {
        String[] s = new String[1024];
        BreakThread bt = new BreakThread(randomNum(), s);
        log l = new log(s);
        bt.start();
        l.setDaemon(true);
        l.start();

    }

    public static String randomNum() {

        int max = 999;
        int min = 100;
        int n = (int) (Math.random() * (max - min + 1) + min);
        String num = String.valueOf(n);
        return num;
    }
}

class BreakThread extends Thread {
    String guessNum;
    String targetNum;
    String[] s;
    int count = 0;

    public BreakThread() {
    }

    public BreakThread(String targetNum) {
        this.targetNum = targetNum;
    }

    public BreakThread(String targetNum, String[] s) {
        this.targetNum = targetNum;
        this.s = s;
    }

    @Override
    public void run() {
        for (char i = '0'; i <= '9'; i++) {
            for (char j = '0'; j <= '9'; j++) {
                for (char k = '0'; k <= '9'; k++) {
                    guessNum = String.valueOf(i) + String.valueOf(j) + String.valueOf(k);
                    System.out.println(guessNum);
                    s[count++] = guessNum;
                    if (guessNum.equals(targetNum)) {
                        System.out.println("解密成功！");
                        try {
                            Thread.sleep(60000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
    }
}

class log extends Thread {
    String[] s;

    public log(String[] s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            FileWriter fw = new FileWriter("log.txt");
            for (int i = 0; i < s.length; i++) {
                fw.write(s[i]);
                fw.write(System.lineSeparator());
                fw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
